﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using SimpleFS.Core;

namespace SimpleFS.Services.Transactions.Implementations
{
    public class Delete : TransactableOperationTemplate
    {
        internal IFileSystemObject target = null;

        internal string originalName = string.Empty;

        public Delete(IFileSystemObject target, IResourceManager resourceManager)
        {
            this.target = target;
            base.resourceManager = resourceManager;
        }

        protected override void Operate()
        {
            if (!this.target.Exists())
            {
                throw new FileNotFoundException(this.target.Name);            
            }
            this.originalName = this.target.Name;
            Console.WriteLine("original name: " + originalName);
            this.target.Rename(this.target.Name + ".shadow");
        }

        public override void Commit()
        {
            this.target.Delete();
        }

        public override void Rollback()
        {
            this.target.Rename(this.originalName);
        }
    }
}
